package com.zz.ecommerce.utils;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <一句话功能描述>
 * <功能详细描述>
 *
 * @author STEVEN
 * @date 2015/12/22,20:40
 */
public class BeanCovertUtil {

    public static <T, E> E swap(T from, E to, Map<String, String> mapper) {
        try {
            if (null == mapper) {
                return swap(from, to);
            }

            Map<String, String> fromMap = BeanUtils.describe(from);
            Map<String, String> toMap = BeanUtils.describe(to);

            for (String key : mapper.keySet()) {
                toMap.put(mapper.get(key), fromMap.get(key));
            }

            BeanUtils.populate(to, toMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return to;
    }

    public static <T, E> E swap(T from, E to) {
        try {
            Map<String, String> fromMap = BeanUtils.describe(from);
            BeanUtils.populate(to, fromMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return to;
    }

    public static <T, E> E swap(T from, Class clazz) {
        E to = null;
        try {
            Constructor constructor = clazz.getConstructor();
            to = (E) constructor.newInstance();
            swap(from, to);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return to;
    }

    public static <T, E> E swap(T from, Class clazz, Map<String, String> mapper) {
        E to = null;
        try {
            if (null == mapper) {
                return swap(from, clazz);
            }

            Constructor constructor = clazz.getConstructor();
            to = (E) constructor.newInstance();
            swap(from, to, mapper);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return to;
    }

    public static <T, E> E swap(T from, Class clazz, String suffix) {
        E to = null;
        try {
            Constructor constructor = clazz.getConstructor();
            to = (E) constructor.newInstance();

            Map<String, String> fromMap = BeanUtils.describe(from);

            Iterator<Map.Entry<String, String>> it = fromMap.entrySet().iterator();

            Map<String, String> mapper = new HashMap<String, String>();
            Map.Entry<String, String> entry;
            while (it.hasNext()) {
                entry = it.next();
                if (entry.getKey().endsWith(suffix)) {
                    mapper.put(entry.getKey().substring(0, entry.getKey().lastIndexOf(suffix)), entry.getValue());
                }
            }

            BeanUtils.populate(to, mapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return to;
    }

    public static <T, E> E swap(T from, E to, String suffix) {
        try {

            Map<String, String> fromMap = BeanUtils.describe(from);

            Iterator<Map.Entry<String, String>> it = fromMap.entrySet().iterator();

            Map<String, String> mapper = new HashMap<String, String>();
            Map.Entry<String, String> entry;
            while (it.hasNext()) {
                entry = it.next();
                if (entry.getKey().endsWith(suffix)) {
                    mapper.put(entry.getKey().substring(0, entry.getKey().lastIndexOf(suffix)), entry.getValue());
                }
            }

            BeanUtils.populate(to, mapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return to;
    }

}
